<?php
/**
 * Created by PhpStorm.
 * User: zhengzifan
 * Date: 2017/3/16
 * Time: 10:10
 */

namespace app\admin\controller;
use think\Cache;

class IndexController extends CommonController {
    public function index(){
        $system_config=db('System')->where(['id'=>['>','0']])->find();
        $this->assign(['domain'=>$this->request->url(true),'system_config'=>$system_config]);
        return $this->fetch('index');
    }

    public function right(){
        $start_time = input('start_time') ? strtotime(input('start_time')) : strtotime(date('Y-m-d',strtotime('-1 month')));
        $end_time = input('end_time') ? strtotime(input('end_time')) : strtotime(date('Y-m-d',time())) + 24 * 3600 -1;

        $start_time_str = date('Y-m-d',$start_time);
        $end_time_str = date('Y-m-d',$end_time);
        $res['time'] = [];
        $res['sales_sum'] = [];
        $res['hires_sum'] = [];
        $res['amount'] = [];
        $res['order'] = [];
        $topData = [];
        $topData['amount_sum'] = 0;
        $topData['sales_sum'] = 0;
        $topData['hires_sum'] = 0;
        $topData['orders_sum'] = 0;
        $topData['orders'] = 0;
        $topData['amounts'] = 0;
        //时间段内每日循环
        $where['o.add_time'] = [['egt',$start_time],['elt',$end_time]];
        $where['o.pay_status'] = 1;
        $store_id = model('AdminStore')->where(['admin_id'=>session('admin')['id']])->column('store_id');
        if(session('admin')['username'] != 'admin'){
            if(!empty($store_id) && count($store_id)>0){
                $where['o.store_id'] = ['in',$store_id];
            }else{
                $where['o.store_id'] = 0;
            }
        }
        $orderList = db('Order')->where($where)
        ->alias('o')
        ->join('__ORDER_GOODS__ og','o.order_id=og.order_id and o.store_id=og.store_id','LEFT')
        ->field('o.order_id,o.add_time,o.order_mode,o.pay_status,o.store_id,o.order_status,o.order_amount,o.shipping_status,o.shipping_status,sum(og.goods_num) as goods_num')
        ->group('o.order_id,o.add_time,o.order_mode,o.pay_status,o.store_id,o.order_status,o.order_amount,o.shipping_status,o.shipping_status')
        ->select();
        $now_str = date('Y-m-d',time());
        $order_sum = 0;
        for($i = $start_time; $i <= $end_time ; $i += 24 * 3600){
            $amount = 0;
            $date_str = date('Y-m-d',$i);
            $order_amount = 0;
            $sale_sum = 0;
            $hire_sum = 0;
            $order_count = 0;
            foreach($orderList as $order){
                $dr_date_str = date('Y-m-d',$order['add_time']);
                if($dr_date_str == $date_str){
                    $order_amount += $order['order_amount'];
                    $order_count ++;
                    if($order['order_mode'] == 0){
                        $sale_sum += $order['goods_num'];
                    }
                    if($order['order_mode'] == 1 && $order['order_status'] >=4){
                        $hire_sum += $order['goods_num'];
                    }
                }
                if($now_str == $date_str && $now_str == $dr_date_str){
                    $order_sum ++;
                    $amount += $order['order_amount'];
                }
            }
            
            if($now_str == $date_str){
                $topData['orders'] = $order_sum ?: 0;
                $topData['amounts'] = $amount ?: 0;
            }
            $topData['amount_sum'] += $order_amount ?: 0;
            $topData['sales_sum'] += $sale_sum ?: 0;
            $topData['hires_sum'] += $hire_sum ?: 0;
            $topData['orders_sum'] += $order_count ?: 0;
            $res['time'][] = date('Y-m-d',$i);
            $res['sales_sum'][] = $sale_sum;
            $res['hires_sum'][] = $hire_sum;
            $res['amount'][] = $order_amount;
            $res['order'][] = $order_count;
        }
        $this->assign([
            'res' => json_encode($res),
            'topData' => $topData
        ]);
        return $this->fetch('right');
    }

    public function getNewOrder(){
        if(model('Order')->where(['order_status'=>0, 'pay_status'=>1])->find()){
            $this->ajaxReturn(['result'=>true]);
        }else{
            $this->ajaxReturn(['result'=>false]);
        }
    }
}